package tsi

import (
	watchmessgae "earth/watch_tcp/watch_message"
	"errors"
	"strconv"

	"github.com/sirupsen/logrus"
)

/*
上传体温数据
终端上传：
[DW*YYYYYYYYYYYYYYY*LEN*temp,X.X]
X.X: 当前测到温度值。
实例：[DW*334588000000156*0009*temp,36.2]
代表终端的温度为：36.2
平台回复：
[DW*334588000000156*0004*temp]
*/

// HealthDataTemp 上传体温数据
type HealthDataTemp struct {
	Temp float64 // 当前测到温度值
}

// DealHealthDataTemp 上传体温数据
func DealHealthDataTemp(watchMessage *watchmessgae.WatchMessage, deviceID string) error {
	// 校验消息长度
	if len(watchMessage.MessageContentList) < 1 {
		logrus.Error("message is too short:", watchMessage.MessageContentList)
		return errors.New("message is too short")
	}
	// 解析消息内容
	healthDataTemp := &HealthDataTemp{}
	temp, err := strconv.ParseFloat(watchMessage.MessageContentList[1], 64)
	if err != nil {
		return err
	}
	healthDataTemp.Temp = temp
	return SendEvent(deviceID, healthDataTemp, "temp")
}
